﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Honestysoft.Financing.Model;
using System.Data.Common;
using Honestysoft.BaseObject;

namespace Honestysoft.Financing.WebUI.Module.Attach.Json
{
    /// <summary>
    /// SaveCabinet 的摘要说明
    /// </summary>
    public class SaveCabinet : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string dataString = context.Request.Params["Data"];
            if (string.IsNullOrEmpty(dataString))
            {
                context.Response.Write("没有需要保存的数据！");
            }
            else
            {
                string[] arrItem = dataString.Split('$');
                List<BD_ProjectFileCabinet> lsCab = new List<BD_ProjectFileCabinet>();
                List<string> lsKey = new List<string>();
                BD_ProjectFileCabinet item = null;
                foreach (string s in arrItem)
                {
                    item = new BD_ProjectFileCabinet();
                    item.ID = s.Split('|')[0];
                    lsKey.Add(item.ID);
                    item.CabinetNo = s.Split('|')[1];
                    lsCab.Add(item);
                }
                DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction();
                DbConnection connection = transaction.Connection;
                try
                {
                    BaseBusinessLogic<BD_ProjectFileCabinet> bll = new BaseBusinessLogic<BD_ProjectFileCabinet>();
                    bll.DeleteByKey(lsKey.ToArray(), transaction);
                    bll.CreateEntitys(lsCab, transaction);
                    transaction.Commit();
                    context.Response.Write("0");
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    context.Response.Write(context.Server.UrlEncode("保存时发生错误！" + ex.Message));
                }
                finally
                {
                    connection.Close();
                }
            }
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}